package com.renjibo.childred.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 任继波
 * Created by Administrator on 2017/2/24.
 */
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.renjibo.childred.R;
import com.renjibo.childred.modle.RecomBrandsBean;
import com.renjibo.childred.utils.PostHead;

import org.xutils.x;

import java.io.File;
import java.util.List;

public class RecomMyExpandableAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<RecomBrandsBean.BrandBean> brand;
    private final LayoutInflater inflater;

    public RecomMyExpandableAdapter(Context context,List<RecomBrandsBean.BrandBean> brand){
        this.brand=brand;
        this.context=context;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getGroupCount() {
        return brand.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return brand.get(groupPosition).getValue().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return brand.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return brand.get(groupPosition).getValue().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroudViewHolder groudViewHolder;
        if(convertView==null){
            groudViewHolder=new GroudViewHolder();
            LinearLayout linearLayout=new LinearLayout(context);
            groudViewHolder.groud_textView=new TextView(context);
            groudViewHolder.groud_textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            groudViewHolder.groud_textView.setTextSize(30);
            groudViewHolder.groud_textView.setTextColor(Color.RED);
            linearLayout.addView(groudViewHolder.groud_textView);
            linearLayout.setBackgroundColor(Color.GREEN);
            convertView=linearLayout;
            convertView.setTag(groudViewHolder);
        }else{
            groudViewHolder= (GroudViewHolder) convertView.getTag();
        }
        groudViewHolder.groud_textView.setText(brand.get(groupPosition).getKey());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
//        LinearLayout layout=new LinearLayout(context);
//        layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        ImageView imageView=new ImageView(context);
//        imageView.setLayoutParams(new ViewGroup.LayoutParams(200,150));
//
//
//        x.image().bind(imageView,s );
//        layout.addView(imageView);
        ChildViewHolder childViewHolder;
        if(convertView==null){
            childViewHolder=new ChildViewHolder();
            convertView=inflater.inflate(R.layout.recom_expandable_item,parent,false);
            childViewHolder.recom_child_imageView= (ImageView) convertView.findViewById(R.id.recom_child_imageView);
            convertView.setTag(childViewHolder);
        }else{
            childViewHolder= (ChildViewHolder) convertView.getTag();
        }
//        ImageView imageView=new ImageView(context);
//        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        String s = PostHead.head + File.separator + brand.get(groupPosition).getValue().get(childPosition).getPic();
//        Log.d("Ren","efwefewfewfwefews"+s);
        x.image().bind(childViewHolder.recom_child_imageView,"http://120.27.126.103:8080/ECServer_D/images/0.png");
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroudViewHolder{
        TextView groud_textView;
    }
    class ChildViewHolder{
        ImageView recom_child_imageView;
    }
}
